import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
import { recordLog } from './utils/api-logger.js'
import Components from './components/index.js'

// 注册全局组件
Vue.use(Components)
Vue.config.productionTip = false

// Vue2 全局错误处理
Vue.config.errorHandler = function (err, vm, info) {
  try {
    recordLog({ type: 'runtime', method: 'APP', url: 'vue2', status: 'ERROR', success: false, errorMessage: (err && err.message) || String(err) + ' | ' + info, durationMs: 0 })
  } catch (e) {}
}

// Web 平台错误捕获
if (typeof window !== 'undefined') {
  window.addEventListener('error', (e) => {
    try { recordLog({ type: 'runtime', method: 'APP', url: 'window.error', status: 'ERROR', success: false, errorMessage: e && e.message, durationMs: 0 }) } catch (e2) {}
  })
  window.addEventListener('unhandledrejection', (e) => {
    try { recordLog({ type: 'runtime', method: 'APP', url: 'window.unhandledrejection', status: 'ERROR', success: false, errorMessage: (e && e.reason && (e.reason.message || e.reason)) || 'unhandledrejection', durationMs: 0 }) } catch (e2) {}
  })
}

App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import { recordLog as recordLog3 } from './utils/api-logger.js'
import Components from './components/index.js'
export function createApp() {
  const app = createSSRApp(App)
  
  // 注册全局组件
  app.use(Components)
  
  // Vue3 全局错误处理
  app.config.errorHandler = (err, instance, info) => {
    try { recordLog3({ type: 'runtime', method: 'APP', url: 'vue3', status: 'ERROR', success: false, errorMessage: (err && err.message) || String(err) + ' | ' + info, durationMs: 0 }) } catch (e) {}
  }
  return {
    app
  }
}
// #endif